<?php

App::uses('AppController', 'Controller');

/**
 * Photos Controller
 *
 * @property Photo $Photo
 */
class PhotosController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Photo->recursive = 0;
        $this->set('photos', $this->paginate());
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        $this->set('photo', $this->Photo->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $upload_handler = new UploadHandler();

            header('Pragma: no-cache');
            header('Cache-Control: private, no-cache');
            header('Content-Disposition: inline; filename="files.json"');
            header('X-Content-Type-Options: nosniff');

            switch ($_SERVER['REQUEST_METHOD']) {
                case 'HEAD':
                case 'GET':
                    $upload_handler->get();
                    break;
                case 'POST':
                    $upload_handler->post();
                    break;
                case 'DELETE':
                    $upload_handler->delete();
                    break;
                case 'OPTIONS':
                    break;
                default:
                    header('HTTP/1.0 405 Method Not Allowed');
            }
            $this->Photo->create();
            if ($this->Photo->save($this->request->data)) {
                $this->Session->setFlash(__('The photo has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
            }
        }
        $photoGroups = $this->Photo->PhotoGroup->find('list');
        $this->set(compact('photoGroups'));
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Photo->save($this->request->data)) {
                $this->Session->setFlash(__('The photo has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Photo->read(null, $id);
        }
        $photoGroups = $this->Photo->PhotoGroup->find('list');
        $this->set(compact('photoGroups'));
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Photo->id = $id;
        if (!$this->Photo->exists()) {
            throw new NotFoundException(__('Invalid photo'));
        }
        if ($this->Photo->delete()) {
            $this->Session->setFlash(__('Photo deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Photo was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
